//
//  SearchResultView.m
//  MyProject
//
//  Created by 石小强 on 2018/4/18.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "SearchResultView.h"

#import "SearchQuestionCell.h"
#import "SearchDecryptCell.h"
#import "SearchWorkmateCell.h"
#import "SearchPositionCell.h"
#import "SearchCompanyCell.h"
#import "BaseViewController.h"

#define tableViewHeight kScreenHeight - NAV_STATUS_H - 40

@interface SearchResultView()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *comprehensiveTV;
@property (nonatomic, strong) UITableView *companyTV;
@property (nonatomic, strong) UITableView *positionTV;
@property (nonatomic, strong) UITableView *workmateTV;
@property (nonatomic, strong) UITableView *questionTV;
@property (nonatomic, strong) UITableView *decryptTV;

@property (nonatomic, strong) UIButton *selectedBtn;

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) NSMutableArray *comprehensiveArray;
@property (nonatomic, strong) NSMutableArray *companyArray;
@property (nonatomic, strong) NSMutableArray *positionArray;
@property (nonatomic, strong) NSMutableArray *workmateArray;
@property (nonatomic, strong) NSMutableArray *questionArray;
@property (nonatomic, strong) NSMutableArray *decryptArray;

@end

@implementation SearchResultView {
    NSInteger _companyPage;
    NSInteger _positionPage;
    NSInteger _workmatePage;
    NSInteger _questionPage;
    NSInteger _decryptPage;
    NSInteger _selectIndex;
    
    BOOL _isRefreshCompany;
    BOOL _isRefreshPosition;
    BOOL _isRefreshWorkmate;
    BOOL _isRefreshQuestion;
    BOOL _isRefreshDecrypt;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];
        
        _companyPage = 1;
        _positionPage = 1;
        _workmatePage = 1;
        _questionPage = 1;
        _decryptPage = 1;
        
        _isRefreshCompany = YES;
        _isRefreshPosition = YES;
        _isRefreshWorkmate = YES;
        _isRefreshQuestion = YES;
        _isRefreshDecrypt = YES;
    }
    return self;
}

/** 加载综合数据 */
- (void)loadComprehensiveData {
    WS(weakSelf)
    [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    
    [SendRequest searchComprehensiveInfoByKey:self.key andPageNo:@"1" result:^(NSDictionary *result, NSError *error) {
        [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索综合数据 = %@", [result objectForKey:@"data"]);
            
            NSArray *positions = [PositionModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"positionInfo"]];
            NSArray *companys = [CompanyModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"companyInfo"]];
            NSArray *workmates = [WorkmateModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"incumbentInfo"]];
            NSArray *decrypts = [DecryptModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"incumbentDecryptInfo"]];
            NSArray *questions = [QuestionModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"questionInfo"]];
            
            [weakSelf.comprehensiveArray removeAllObjects];
            [weakSelf.comprehensiveArray addObject:companys];
            [weakSelf.comprehensiveArray addObject:positions];
            [weakSelf.comprehensiveArray addObject:workmates];
            [weakSelf.comprehensiveArray addObject:questions];
            [weakSelf.comprehensiveArray addObject:decrypts];
            
            [weakSelf.comprehensiveTV.mj_header endRefreshing];
            [weakSelf.comprehensiveTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 加载企业数据 */
- (void)loadCompanyData {
    WS(weakSelf)
    [weakSelf.companyTV haveDataRemoveEmptyUI];
    
    if (_isRefreshCompany) {
        [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    }
    
    [SendRequest getCompanyInfoWithKey:self.key andPageNo:[NSString stringWithFormat:@"%zd", _companyPage] result:^(NSDictionary *result, NSError *error) {
        if (_isRefreshCompany) {
            [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索企业数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *companys = [CompanyModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_companyPage == 1) {
                [weakSelf.companyArray removeAllObjects];
                [weakSelf.companyArray addObjectsFromArray:companys];
                
                [weakSelf.companyTV.mj_header endRefreshing];
                
                if (companys.count == 0) {
                    [weakSelf.companyTV noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                    [weakSelf.companyTV.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.companyTV haveDataRemoveEmptyUI];
                    [weakSelf.companyTV.mj_footer endRefreshing];
                }
            } else {
                [weakSelf.companyArray addObjectsFromArray:companys];
                
                if (companys.count == kNumOfPageIntSize) {
                    [weakSelf.companyTV.mj_footer endRefreshing];
                } else {
                    [weakSelf.companyTV.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            [weakSelf.companyTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 加载职位数据 */
- (void)loadPositionData {
    WS(weakSelf)
    [weakSelf.positionTV haveDataRemoveEmptyUI];
    
    if (_isRefreshPosition) {
        [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    }
    [SendRequest searchPositionInfoByKey:self.key andPageNo:[NSString stringWithFormat:@"%zd", _positionPage] result:^(NSDictionary *result, NSError *error) {
        if (_isRefreshPosition) {
            [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索职位数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *positions = [PositionModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_positionPage == 1) {
                [weakSelf.positionArray removeAllObjects];
                [weakSelf.positionArray addObjectsFromArray:positions];
                
                [weakSelf.positionTV.mj_header endRefreshing];
                
                if (positions.count == 0) {
                    [weakSelf.positionTV noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                    [weakSelf.positionTV.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.positionTV haveDataRemoveEmptyUI];
                    [weakSelf.positionTV.mj_footer endRefreshing];
                }
            } else {
                [weakSelf.positionArray addObjectsFromArray:positions];
                if (positions.count == kNumOfPageIntSize) {
                    [weakSelf.positionTV.mj_footer endRefreshing];
                } else {
                    [weakSelf.positionTV.mj_footer endRefreshingWithNoMoreData];
                }
            }

            [weakSelf.positionTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 加载同事数据 */
- (void)loadWorkmateData {
    WS(weakSelf)
    [weakSelf.workmateTV haveDataRemoveEmptyUI];
    
    if (_isRefreshWorkmate) {
        [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    }
    [SendRequest searchIncumbentInfoByKey:self.key andPageNo:[NSString stringWithFormat:@"%zd", _workmatePage] result:^(NSDictionary *result, NSError *error) {
        if (_isRefreshWorkmate) {
            [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索同事数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *workmates = [WorkmateModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_workmatePage == 1) {
                [weakSelf.workmateArray removeAllObjects];
                [weakSelf.workmateArray addObjectsFromArray:workmates];
                
                [weakSelf.workmateTV.mj_header endRefreshing];
                
                if (workmates.count == 0) {
                    [weakSelf.workmateTV noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                    [weakSelf.workmateTV.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.workmateTV haveDataRemoveEmptyUI];
                    [weakSelf.workmateTV.mj_footer endRefreshing];
                }
            } else {
                [weakSelf.workmateArray addObjectsFromArray:workmates];
                if (workmates.count == kNumOfPageIntSize) {
                    [weakSelf.workmateTV.mj_footer endRefreshing];
                } else {
                    [weakSelf.workmateTV.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            [weakSelf.workmateTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 加载问题数据 */
- (void)loadQuestionData {
    WS(weakSelf)
    [weakSelf.questionTV haveDataRemoveEmptyUI];
    
    if (_isRefreshQuestion) {
        [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    }
    [SendRequest searchQuestionInfoByKey:self.key andPageNo:[NSString stringWithFormat:@"%zd", _questionPage] result:^(NSDictionary *result, NSError *error) {
        if (_isRefreshQuestion) {
            [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索问题数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *questions = [QuestionModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_questionPage == 1) {
                [weakSelf.questionArray removeAllObjects];
                [weakSelf.questionArray addObjectsFromArray:questions];
                
                [weakSelf.questionTV.mj_header endRefreshing];
                
                if (questions.count == 0) {
                    [weakSelf.questionTV noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                    [weakSelf.questionTV.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.questionTV haveDataRemoveEmptyUI];
                    [weakSelf.questionTV.mj_footer endRefreshing];
                }
            } else {
                [weakSelf.questionArray addObjectsFromArray:questions];
                if (questions.count == kNumOfPageIntSize) {
                    [weakSelf.questionTV.mj_footer endRefreshing];
                } else {
                    [weakSelf.questionTV.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            [weakSelf.questionTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

/** 加载解密数据 */
- (void)loadDecryptData {
    WS(weakSelf)
    [weakSelf.decryptTV haveDataRemoveEmptyUI];
    
    if (_isRefreshDecrypt) {
        [(BaseViewController *)[Tool topViewController] showProgressWithView:self animated:YES];
    }
    [SendRequest searchIncumbentDecryptInfoByKey:self.key andPageNo:[NSString stringWithFormat:@"%zd", _decryptPage] result:^(NSDictionary *result, NSError *error) {
        if (_isRefreshDecrypt) {
            [(BaseViewController *)[Tool topViewController] hideProgress:self animated:YES];
        }
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"搜索解密数据 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *decrypts = [DecryptModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_decryptPage == 1) {
                [weakSelf.decryptArray removeAllObjects];
                [weakSelf.decryptArray addObjectsFromArray:decrypts];
                
                [weakSelf.decryptTV.mj_header endRefreshing];
                
                if (decrypts.count == 0) {
                    [weakSelf.decryptTV noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                    [weakSelf.decryptTV.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [weakSelf.decryptTV haveDataRemoveEmptyUI];
                    [weakSelf.decryptTV.mj_footer endRefreshing];
                }
            } else {
                [weakSelf.decryptArray addObjectsFromArray:decrypts];
                if (decrypts.count == kNumOfPageIntSize) {
                    [weakSelf.decryptTV.mj_footer endRefreshing];
                } else {
                    [weakSelf.decryptTV.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            [weakSelf.decryptTV reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


- (void)initSubViews {
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_topView];
    
    _bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - NAV_STATUS_H - 40)];
    _bottomScrollView.contentSize = CGSizeMake(kScreenWidth * 6, 0);
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.delegate = self;
    [self addSubview:_bottomScrollView];
    
    self.titles = @[@"综合", @"企业", @"职位", @"同事", @"问题", @"解密"];
    [self setupTopViewTitles:self.titles];
    
    [self setupTableViews];
}

- (void)setSelectIndex:(NSInteger)index {
    _selectIndex = index;
    [self.bottomScrollView setContentOffset:CGPointMake(kScreenWidth * index, 0)];
}

- (void)setupTableViews {
    _comprehensiveTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, tableViewHeight)];
    _comprehensiveTV.dataSource = self;
    _comprehensiveTV.delegate = self;
    _comprehensiveTV.backgroundColor = kStyleColor;
    _comprehensiveTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_comprehensiveTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchCompanyCell class]) bundle:nil] forCellReuseIdentifier:@"searchCompanyCell"];
    [_comprehensiveTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchPositionCell class]) bundle:nil] forCellReuseIdentifier:@"searchPositionCell"];
    [_comprehensiveTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchWorkmateCell class]) bundle:nil] forCellReuseIdentifier:@"searchWorkmateCell"];
    [_comprehensiveTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchQuestionCell class]) bundle:nil] forCellReuseIdentifier:@"SearchQuestionCell"];
    [_comprehensiveTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchDecryptCell class]) bundle:nil] forCellReuseIdentifier:@"SearchDecryptCell"];
    [_bottomScrollView addSubview:_comprehensiveTV];
    
    _companyTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, tableViewHeight)];
//    _companyTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, tableViewHeight) style:UITableViewStyleGrouped];
    _companyTV.dataSource = self;
    _companyTV.delegate = self;
    _companyTV.backgroundColor = kStyleColor;
    _companyTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_companyTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchCompanyCell class]) bundle:nil] forCellReuseIdentifier:@"searchCompanyCell"];
    [_bottomScrollView addSubview:_companyTV];
    
    _positionTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth * 2, 0, kScreenWidth, tableViewHeight)];
    _positionTV.dataSource = self;
    _positionTV.delegate = self;
    _positionTV.backgroundColor = kStyleColor;
    _positionTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_positionTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchPositionCell class]) bundle:nil] forCellReuseIdentifier:@"searchPositionCell"];
    [_bottomScrollView addSubview:_positionTV];
    
    _workmateTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth * 3, 0, kScreenWidth, tableViewHeight)];
    _workmateTV.dataSource = self;
    _workmateTV.delegate = self;
    _workmateTV.backgroundColor = kStyleColor;
    _workmateTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_workmateTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchWorkmateCell class]) bundle:nil] forCellReuseIdentifier:@"searchWorkmateCell"];
    [_bottomScrollView addSubview:_workmateTV];
    
    _questionTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth * 4, 0, kScreenWidth, tableViewHeight)];
    _questionTV.dataSource = self;
    _questionTV.delegate = self;
    _questionTV.backgroundColor = kStyleColor;
    _questionTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_questionTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchQuestionCell class]) bundle:nil] forCellReuseIdentifier:@"SearchQuestionCell"];
    [_bottomScrollView addSubview:_questionTV];
    
    _decryptTV = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth * 5, 0, kScreenWidth, tableViewHeight)];
    _decryptTV.dataSource = self;
    _decryptTV.delegate = self;
    _decryptTV.backgroundColor = kStyleColor;
    _decryptTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_decryptTV registerNib:[UINib nibWithNibName:NSStringFromClass([SearchDecryptCell class]) bundle:nil] forCellReuseIdentifier:@"SearchDecryptCell"];
    [_bottomScrollView addSubview:_decryptTV];
    
    WS(weakSelf)
    _comprehensiveTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadComprehensiveData];
    }];
    _companyTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _companyPage = 1;
        _isRefreshCompany = NO;
        [weakSelf.companyTV haveDataRemoveEmptyUI];
        [weakSelf loadCompanyData];
    }];
    _companyTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _companyPage += 1;
        _isRefreshCompany = NO;
        [weakSelf loadCompanyData];
    }];
    _positionTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _positionPage = 1;
        _isRefreshPosition = NO;
        [weakSelf.positionTV haveDataRemoveEmptyUI];
        [weakSelf loadPositionData];
    }];
    _positionTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _positionPage += 1;
        _isRefreshPosition = NO;
        [weakSelf loadPositionData];
    }];
    _workmateTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _workmatePage = 1;
        _isRefreshWorkmate = NO;
        [weakSelf.workmateTV haveDataRemoveEmptyUI];
        [weakSelf loadWorkmateData];
    }];
    _workmateTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _workmatePage += 1;
        _isRefreshWorkmate = NO;
        [weakSelf loadWorkmateData];
    }];
    _questionTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _questionPage = 1;
        _isRefreshQuestion = NO;
        [weakSelf.questionTV haveDataRemoveEmptyUI];
        [weakSelf loadQuestionData];
    }];
    _questionTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _questionPage += 1;
        _isRefreshQuestion = NO;
        [weakSelf loadQuestionData];
    }];
    _decryptTV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _decryptPage = 1;
        _isRefreshDecrypt = NO;
        [weakSelf.decryptTV haveDataRemoveEmptyUI];
        [weakSelf loadDecryptData];
    }];
    _decryptTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _decryptPage += 1;
        _isRefreshDecrypt = NO;
        [weakSelf loadDecryptData];
    }];
}


#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.comprehensiveTV) {
        return self.comprehensiveArray.count;
    } else {
        return 1;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == self.comprehensiveTV) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
        headerView.backgroundColor = kStyleColor;
        UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 30)];
        titleL.text = [self.titles objectAtIndex:section + 1];
        titleL.font = [UIFont fontWithName:@"PingFang SC" size:11];
        titleL.textColor = UIColorFromRGB(0x869BA6);
        [headerView addSubview:titleL];
        return headerView;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == self.comprehensiveTV) {
        return 30;
    } else {
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.comprehensiveTV) {
        NSArray *subs = [self.comprehensiveArray objectAtIndex:section];
        return subs.count;
    } else if (tableView == self.companyTV) {
        return self.companyArray.count;
    } else if (tableView == self.positionTV) {
        return self.positionArray.count;
    } else if (tableView == self.workmateTV) {
        return self.workmateArray.count;
    } else if (tableView == self.questionTV) {
        return self.questionArray.count;
    } else if (tableView == self.decryptTV) {
        return self.decryptArray.count;
    }
    else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.companyTV) {
        SearchCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCompanyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        CompanyModel *company = [self.companyArray objectAtIndex:indexPath.row];
        cell.model = company;
        return cell;
    } else if (tableView == self.positionTV) {
        SearchPositionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchPositionCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        PositionModel *position = [self.positionArray objectAtIndex:indexPath.row];
        cell.model = position;
        return cell;
    } else if (tableView == self.workmateTV) {
        SearchWorkmateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchWorkmateCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        WorkmateModel *workmate = [self.workmateArray objectAtIndex:indexPath.row];
        cell.model = workmate;
        return cell;
    } else if (tableView == self.questionTV) {
        SearchQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchQuestionCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        QuestionModel *question = [self.questionArray objectAtIndex:indexPath.row];
        cell.model = question;
        return cell;
    } else if (tableView == self.decryptTV) {
        SearchDecryptCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchDecryptCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        DecryptModel *decry = [self.decryptArray objectAtIndex:indexPath.row];
        cell.model = decry;
        return cell;
    } else {
        if (indexPath.section == 0) {
            SearchCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchCompanyCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *companys = [self.comprehensiveArray objectAtIndex:indexPath.section];
            cell.model = [companys objectAtIndex:indexPath.row];
            return cell;
        } else if (indexPath.section == 1) {
            SearchPositionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchPositionCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *positions = [self.comprehensiveArray objectAtIndex:indexPath.section];
            cell.model = [positions objectAtIndex:indexPath.row];
            return cell;
        } else if (indexPath.section == 2) {
            SearchWorkmateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchWorkmateCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *workmates = [self.comprehensiveArray objectAtIndex:indexPath.section];
            cell.model = [workmates objectAtIndex:indexPath.row];
            return cell;
        } else if (indexPath.section == 3) {
            SearchQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchQuestionCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *questions = [self.comprehensiveArray objectAtIndex:indexPath.section];
            cell.model = [questions objectAtIndex:indexPath.row];
            return cell;
        } else if (indexPath.section == 4) {
            SearchDecryptCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchDecryptCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSArray *decrypts = [self.comprehensiveArray objectAtIndex:indexPath.section];
            cell.model = [decrypts objectAtIndex:indexPath.row];
            return cell;
        } else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = [NSString stringWithFormat:@"indexPath.row = %zd", indexPath.row];
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.companyTV) {
        return 104;
    } else if (tableView == self.positionTV) {
        return 104;
    } else if (tableView == self.workmateTV) {
        return 104;
    } else if (tableView == self.questionTV) {
        
        QuestionModel *model = [self.questionArray objectAtIndex:indexPath.row];
        CGSize contentS = [Tool sizeWithText:model.content font:[UIFont fontWithName:@"PingFang SC" size:14]];
        if (contentS.width > kScreenWidth - 60) {
            return 104;
        } else {
            return 104 - 16;
        }
        
//        return 104;
    } else if (tableView == self.decryptTV) {
        return 117;
    } else {
        if (indexPath.section == 4) {
            if (indexPath.row == 0) {
                return 102;
            } else {
                return 117;
            }
        } else {
            if (indexPath.row == 0) {
                return 89;
            } else {
                return 104;
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.companyTV) {
        CompanyModel *company = [self.companyArray objectAtIndex:indexPath.row];
        self.companyBlock(company);
    } else if (tableView == self.positionTV) {
        PositionModel *position = [self.positionArray objectAtIndex:indexPath.row];
        self.positionBlock(position);
    } else if (tableView == self.workmateTV) {
        WorkmateModel *workmate = [self.workmateArray objectAtIndex:indexPath.row];
        self.workmateBlock(workmate);
    } else if (tableView == self.questionTV) {
        QuestionModel *question = [self.questionArray objectAtIndex:indexPath.row];
        self.questionBlock(question);
    } else if (tableView == self.decryptTV) {
        DecryptModel *decry = [self.decryptArray objectAtIndex:indexPath.row];
        self.decryptBlock(decry);
    } else {
        if (indexPath.section == 0) {
            NSArray *companys = [self.comprehensiveArray objectAtIndex:indexPath.section];
            CompanyModel *company = [companys objectAtIndex:indexPath.row];
            self.companyBlock(company);
        } else if (indexPath.section == 1) {
            NSArray *positions = [self.comprehensiveArray objectAtIndex:indexPath.section];
            PositionModel *position = [positions objectAtIndex:indexPath.row];
            self.positionBlock(position);
        } else if (indexPath.section == 2) {
            NSArray *workmates = [self.comprehensiveArray objectAtIndex:indexPath.section];
            WorkmateModel *workmate = [workmates objectAtIndex:indexPath.row];
            self.workmateBlock(workmate);
        } else if (indexPath.section == 3) {
            NSArray *questions = [self.comprehensiveArray objectAtIndex:indexPath.section];
            QuestionModel *question = [questions objectAtIndex:indexPath.row];
            self.questionBlock(question);
        } else if (indexPath.section == 4) {
            NSArray *decrypts = [self.comprehensiveArray objectAtIndex:indexPath.section];
            DecryptModel *decry = [decrypts objectAtIndex:indexPath.row];
            self.decryptBlock(decry);
        } else {
        }
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.bottomScrollView) {
        NSInteger index = scrollView.contentOffset.x / kScreenWidth + 0.5;
        
        [self setSelectedBtnWithIndex:index];
        
        if (index != _selectIndex) {
            _selectIndex = index;
            [self loadDataWithIndex:index];
        }
    }
}

- (void)setupTopViewTitles:(NSArray *)titles {
    CGFloat itemW = kScreenWidth / titles.count;
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i * itemW, 0, itemW, 40)];
        [btn setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x686563) forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0x1AA9BA) forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:15];
        btn.tag = i;
        [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:btn];
        
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;
        }
    }
    
    _topIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, itemW, 2)];
    _topIndicatorView.backgroundColor = UIColorFromRGB(0x29AEBE);
    [_topView addSubview:_topIndicatorView];
}

- (void)titleBtnClick:(UIButton *)btn {
    if (btn.selected) {
        return;
    }
    
    [self.bottomScrollView setContentOffset:CGPointMake(btn.tag * kScreenWidth, 0) animated:YES];
}

- (void)setSelectedBtnWithIndex:(NSInteger)index {
    for (UIView *con in self.topView.subviews) {
        if ([con isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)con;
            if (btn.tag == index) {
                self.selectedBtn.selected = NO;
                btn.selected = YES;
                self.selectedBtn = btn;
            } else {
                btn.selected = NO;
            }
        }
    }
    
    CGFloat itemW = kScreenWidth / 6;
    _topIndicatorView.frame = CGRectMake(itemW * self.selectedBtn.tag, 38, itemW, 2);
}

- (void)loadDataWithIndex:(NSInteger)index {
    if (index == 0) {
        if (self.comprehensiveArray.count > 0) {
            return;
        } else {
            // 加载综合数据
        }
    } else if (index == 1) {
        if (self.companyArray.count > 0) {
            return;
        } else {
            _companyPage = 1;
            [self loadCompanyData];
        }
    } else if (index == 2) {
        if (self.positionArray.count > 0) {
            return;
        } else {
            _positionPage = 1;
            [self loadPositionData];
        }
    } else if (index == 3) {
        if (self.workmateArray.count > 0) {
            return;
        } else {
            _workmatePage = 1;
            [self loadWorkmateData];
        }
    } else if (index == 4) {
        if (self.questionArray.count > 0) {
            return;
        } else {
            _questionPage = 1;
            [self loadQuestionData];
        }
    } else if (index == 5) {
        if (self.decryptArray.count > 0) {
            return;
        } else {
            _decryptPage = 1;
            [self loadDecryptData];
        }
    } else {
        return;
    }
}

- (void)clearData {
    [self.comprehensiveArray removeAllObjects];
    [self.companyArray removeAllObjects];
    [self.positionArray removeAllObjects];
    [self.workmateArray removeAllObjects];
    [self.questionArray removeAllObjects];
    [self.decryptArray removeAllObjects];
    
    [self.comprehensiveTV reloadData];
    [self.companyTV reloadData];
    [self.positionTV reloadData];
    [self.workmateTV reloadData];
    [self.questionTV reloadData];
    [self.decryptTV reloadData];
}

- (NSMutableArray *)comprehensiveArray {
    if (!_comprehensiveArray) {
        _comprehensiveArray = [[NSMutableArray alloc] init];
    }
    return _comprehensiveArray;
}

- (NSMutableArray *)companyArray {
    if (!_companyArray) {
        _companyArray = [[NSMutableArray alloc] init];
    }
    return _companyArray;
}

- (NSMutableArray *)positionArray {
    if (!_positionArray) {
        _positionArray = [[NSMutableArray alloc] init];
    }
    return _positionArray;
}

- (NSMutableArray *)workmateArray {
    if (!_workmateArray) {
        _workmateArray = [[NSMutableArray alloc] init];
    }
    return _workmateArray;
}

- (NSMutableArray *)questionArray {
    if (!_questionArray) {
        _questionArray = [[NSMutableArray alloc] init];
    }
    return _questionArray;
}

- (NSMutableArray *)decryptArray {
    if (!_decryptArray) {
        _decryptArray = [[NSMutableArray alloc] init];
    }
    return _decryptArray;
}

@end
